package so.sao.code.service.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import so.sao.code.service.domain.CodeType;
import so.sao.code.service.domain.ThirdPartyCodeFileInfoProvider;
import so.sao.code.service.domain.UserBaseInfoWrapper;
import so.sao.code.service.infrastructure.model.vo.ThirdPartCodeFileVO;
import so.sao.code.service.infrastructure.stream.ProduceManager;
import so.sao.sso.feign.dto.IsvUserDTO;

/**
 * 
 * @author wei.wang
 *
 */
// TODO 待整理
@Service("thirdPartyCodeFileMappingHandle")
public class ThirdPartyCodeFileMappingHandleServiceImpl extends AbstractCodeFilePackageHandleService {

	private static Logger logger = LoggerFactory.getLogger(ThirdPartyCodeFileMappingHandleServiceImpl.class);
	
	public void validate(IsvUserDTO user, MultipartFile file, ThirdPartCodeFileVO rex) {
		// 简单检查
		if (file.isEmpty() || file.getSize() <= 0) {
			throwException(null, "文件不能为空");
		}
		String originalFilename = file.getOriginalFilename();
		if (!originalFilename.endsWith(rex.getFileType())) {
			throwException(null, "文件类型不一致");
		}

		if (rex.getMd5() == null || rex.getMd5().length() == 0) {
			throwException(null, "md5 为空");
		}

		if (rex.getCodeType() == null || rex.getCodeType() != CodeType.MAPPING.getValue()) {
			throwException(null, "codeType 非法");
		}

		if (user == null || user.getEseId() == null || user.getEseId().equals(0L)) {
			throwException(null, "非法用户");
		}
		
		if (user == null || user.getCompanyId() == null || user.getCompanyId().equals(0L)) {
			throwException(null, "Company Id/code 无效");
		}
	}
	
	public ThirdPartyCodeFileInfoProvider saveFile(UserBaseInfoWrapper usebaseInfo,
			ThirdPartyCodeFileInfoProvider wrapper, MultipartFile file) {
		try {
			ThirdPartyCodeFileInfoProvider saveFile = super.saveFile(usebaseInfo, wrapper, file);

			pushSignmatrix(usebaseInfo, wrapper);
			return saveFile;
		} finally {
			deleteFile(wrapper.getLocalPath());
		}
	}
	
	@Override
	public void doProcess(UserBaseInfoWrapper user, ThirdPartyCodeFileInfoProvider wrapper) {
		throwException(null, "待实现");
	}

	@Override
	public void doAsyncProcess(UserBaseInfoWrapper user, ThirdPartyCodeFileInfoProvider wrapper) {
		throwException(null, "待实现");
	}

}
